package com.orangelife.mobile.myhouse.activity;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.curry.android.http.VolleyHttpUtil;
import com.curry.orangelife.mobile.R;
import com.orangelife.mobile.common.activity.OrangeLifeBaseActivity;
import com.common.jump.HouseJumper;
import com.orangelife.mobile.common.view.EmptyLayout;
import com.orangelife.mobile.myhouse.adapter.ResidentGvAdapter;
import com.orangelife.mobile.myhouse.biz.HouseHttpBiz;
import com.orangelife.mobile.myhouse.entity.HouseBean;
import com.orangelife.mobile.myhouse.entity.ResidentBean2;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 住户列表
 * <p/>
 * Created by 吴格非 on 2015/11/13.
 */
public class ResidentListActivity extends OrangeLifeBaseActivity {

    @Bind(R.id.gvResident)
    GridView gvResident;
    @Bind(R.id.tvIdentity)
    TextView tvIdentity;
    @Bind(R.id.llRoot)
    LinearLayout llRoot;
    @Bind(R.id.emptyLayout)
    EmptyLayout emptyLayout;
    @Bind(R.id.ib_title_left)
    LinearLayout ibTitleLeft;
    @Bind(R.id.tv_title)
    TextView tvTitle;

    private ResidentGvAdapter adapter;
    private List<ResidentBean2> mList = new ArrayList<>();
    private String apartmentID;
    private String identityCode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_resident_list);
        ButterKnife.bind(this);
        setAdapter();
        getIntentInfo();
        initTitle();
        initView();
        setListener();
    }

    private void initTitle() {
        tvTitle.setText("我的房屋");
    }

    private void initView() {
        gvResident.setSelector(new ColorDrawable(Color.TRANSPARENT));
    }

    private void setListener() {
        emptyLayout.setOnLayoutClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                requestResidentList();
            }
        });

        llRoot.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                adapter.isShowDelIcon(false);
                return false;
            }
        });
    }

    private void getIntentInfo() {
        ArrayList<String> list = getIntent().getStringArrayListExtra(HouseJumper.KEY_RESIDENT);
        if (list != null && list.size() > 1) {
            identityCode = list.get(0);
            apartmentID = list.get(1);
            if (HouseBean.ROLE_TYPE_FAMILY_CODE.equals(identityCode)) {
                tvIdentity.setText("家属");
            } else {
                tvIdentity.setText("租客");
            }
            requestResidentList();
        } else {/*异常*/
            emptyLayout.dismiss();
            emptyLayout.setErrorType(EmptyLayout.NODATA);
            emptyLayout.setOnLayoutClickListener(null);
        }
    }

    private void setAdapter() {
        adapter = new ResidentGvAdapter(this);
        gvResident.setAdapter(adapter);
        adapter.setList(mList);
    }

    private void requestResidentList() {
        HouseHttpBiz.getResidentList(new VolleyHttpUtil.ResponseCallBack() {
            @Override
            public void handleResponse(JSONObject response, int errCode) {
                emptyLayout.dismiss();
                mList = HouseHttpBiz.handleResidentList(response);
                if (mList != null) {
                    if (mList.size() == 0) {
                        emptyLayout.setErrorType(EmptyLayout.NODATA);
                    } else {
                        adapter.setList(mList);
                        adapter.notifyDataSetChanged();
                    }
                }
            }
        }, apartmentID, identityCode, 200, 1);
    }
}
